package com.bsu.ascm.tool;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bsu.ascm.R;

public class Utils {
	private static ProgressDialog m_Dialog;
	public static void appLogin(Activity a){
		final Activity activity = a;
		LayoutInflater factory = LayoutInflater.from(activity);
		final View DialogView = factory.inflate(R.layout.login, null);
		Dialog dlg = new AlertDialog.Builder(activity)
			.setTitle("登录框")
			.setView(DialogView)
			.setPositiveButton("确定", 
				new DialogInterface.OnClickListener() 
				{
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
						m_Dialog = ProgressDialog.show(activity, "请稍等...", "正在为你登录...",true);
						new Thread(){
							public void run(){
								try{
									sleep(3000);
								}catch(Exception e){
									e.printStackTrace();
								}
								finally{
									m_Dialog.dismiss();
								}
							}
						}.start();
					}
				})
			.setNegativeButton("取消", 
				new DialogInterface.OnClickListener()
				{
					public void onClick(DialogInterface dialog,int whichButton){
						activity.finish();
					}
				}
			).create();
			dlg.show();
	}
	
	/*
	 * 退出界面
	 */
	public static void appExit(Activity a){
		final Activity activity = a;
		Builder dialog = new AlertDialog.Builder(a);
		dialog
			.setTitle("退出程序")
			.setMessage("确定要退出程序？")
			.setPositiveButton("确定", new OnClickListener(){
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
		    		android.os.Process.killProcess(android.os.Process.myPid());
					//activity.finish();
				}
			})
			.setNeutralButton("取消", new OnClickListener(){
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					dialog.dismiss();
				}
			}).show();
	}
	/**
	 * 获得当前ContentView的ID
	 * @param activity	Activity对象
	 * @return	返回ContentView的Id
	 */
	public static int getContentViewId(Activity activity){
		ViewGroup gview = (ViewGroup)activity.getWindow().getDecorView();
		View content = gview.getChildAt(0);
		return content.getId();
	}
	/**
	 * 生成带String数据类型的Message
	 * @param key	数据名
	 * @param msg	字符串数据 
	 * @return		返回发送的消息对象
	 */
	public static Message makeMessage(String key,String msg){
		Bundle bundle = new Bundle();
		bundle.putString(key, msg);
		Message retmsg = new Message();
		retmsg.setData(bundle);
		return retmsg;
	}
	/**
	 * 生成带int数据类型的Message
	 * @param key	数据名
	 * @param msg	整型数据
	 * @return		返回发送的消息对象
	 */
	public static Message makeMessage(String key,int msg){
		Bundle bundle = new Bundle();
		bundle.putInt(key, msg);
		Message retmsg = new Message();
		retmsg.setData(bundle);
		return retmsg;
	}
	/**
	 * 获得s在sa中的位置
	 * @param sa	包含s的字符串数组
	 * @param s		要判断的字符串
	 * @return		返回s在sa中的位置，如果s在sa中不存在，返回-1
	 */
	public static int getDataPosition(String[] psa,String ps)
	{
		for(int i=0;i<psa.length;i++)
			if(psa[i].equals(ps))
				return i;
		return -1;
	}
	/**
	 * 通过value获得key
	 * @param hs	要查找的HashMap
	 * @param value	value值
	 * @return		返回对应的key
	 */
	public static String getKeyByValue(HashMap<String,String> hs,String value){
		if(!hs.containsValue(value))
			return null;
		Set<Entry<String,String>> se = hs.entrySet();
		Iterator<Entry<String,String>>it = se.iterator();
		while(it.hasNext()){
			Entry<String,String> e = it.next();
			if(value.equals(e.getValue()))
				return e.getKey();
		}
		return null;
	}
	/**
	 * 通过value从List<Pair>中获得Key值
	 * @param lp	List<Pair>集合
	 * @param value	value值
	 * @return		返回对应key值
	 */
	public static String getLPKey(List<Pair> lp,String value){
		for(Pair p:lp)
			if(p.getValue().equals(value))
				return p.getKey();
		return null;
	}
	/**
	 * 通过key从List<Pair>中获得value值
	 * @param lp	List<Pair>集合
	 * @param key 	value值
	 * @return		返回对应key值
	 */
	public static String getLPValue(List<Pair> lp,String key){
		for(Pair p:lp)
			if(p.getKey().equals(key))
				return p.getValue();
		return null;
	}
}
